//
//  main.m
//  5-15复习
//
//  Created by 钟纪旺 on 16/5/15.
//  Copyright © 2016年 zjw. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Information.h"
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        NSString * path = @"/Users/zhongjiwang/Desktop/cbv.vcf";
        NSError *err;
        NSString *vcf =[[NSString alloc]initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&err];
        if (err!=0) {
            NSLog(@"%@",err);
            
        }
        NSArray *arr =[vcf componentsSeparatedByString:@"\n"];
            
            NSMutableArray *abc =[NSMutableArray new];
            NSMutableDictionary *diti;
        for (NSString *line in arr) {
            if ([line hasPrefix:@"BEGIN"]) {
                NSLog(@"开始");
                diti =[NSMutableDictionary new];
            }if([line hasPrefix:@"END"]){
                NSLog(@"结束");
                [abc addObject:diti];
            }if ([line hasPrefix:@"TEL"]) {
                NSArray *tels =[line componentsSeparatedByString:@":"];
                NSString *last =tels.lastObject;
                diti [@"tel"]=last;
                
                
            }if ([line hasPrefix:@"FN"]) {
                NSArray *names = [line  componentsSeparatedByString:@":"];
                NSString *name =names.lastObject;
                name = [name stringByReplacingOccurrencesOfString:@"=" withString:@"%"];
                name = name.stringByRemovingPercentEncoding;
                
                NSLog(@"名字:%@",name);
                diti[@"name"]= name;
            
            }
                }
                [abc writeToFile:@"/Users/zhongjiwang/Desktop/abc2.txt" atomically:NO];
                NSData *data = [NSJSONSerialization dataWithJSONObject:abc options:NSJSONWritingPrettyPrinted error:nil];
                [data writeToFile:@"/Users/zhongjiwang/Desktop/abc.txt" atomically:NO];
                
                
                NSString *str =[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
                NSLog(@"%@",str);
                
                NSMutableArray *res =[NSMutableArray new];
                for (NSDictionary * diti in abc) {
                    Information *p =[Information new];
                    [p setValuesForKeysWithDictionary:diti];
                    [res addObject:p];
                
                NSLog(@"%@",res);
    
                
        }
        }
            return 0;

}
